package com.flowable.common.domain;

import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

/**
 * 响应信息主体
 *
 * @author chenyan
 */
public class R<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 成功
     */
    public static final int SUCCESS = 200;

    /**
     * 失败
     */
    public static final int FAIL = 0;

    /**
     * 登录失效
     */
    public static final int LOGOUT = -1;

    @ApiModelProperty(value = "服务器返回状态码", name = "code", example = "200", required = true)
    private int code;
    @ApiModelProperty(value = "服务状态码中文说明", name = "msg", example = "成功", required = true)
    private String msg = "";
    @ApiModelProperty(value = "返回对象", example = "Test")
    private T data;

    private long time = System.currentTimeMillis() / 1000;

    /**
     * 判断结果是否为1响应
     *
     * @return
     */
    /*public boolean isResultSuccess() {
        if (getCode() == HttpStatus.SUCCESS) {
            return true;
        }
        return false;
    }*/

    /**
     * 判断结果是否为500响应
     *
     * @return
     */
   /* public boolean isResultError() {
        if (getCode() == HttpStatus.ERROR) {
            return true;
        }
        return false;
    }*/
    public static <T> R<T> ok() {
        return restResult(null, SUCCESS, "成功");
    }

    public static <T> R<T> ok(T data) {
        return restResult(data, SUCCESS, "成功");
    }

    public static <T> R<T> ok(T data, String msg) {
        return restResult(data, SUCCESS, msg);
    }

    public static <T> R<T> fail() {
        return restResult(null, FAIL, null);
    }

    public static <T> R<T> logOut() {
        return restResult(null, LOGOUT, "登录失效，请重新登录");
    }

    public static <T> R<T> fail(String msg) {
        return restResult(null, FAIL, msg);
    }

    public static <T> R<T> fail(T data) {
        return restResult(data, FAIL, null);
    }

    public static <T> R<T> fail(T data, String msg) {
        return restResult(data, FAIL, msg);
    }

    public static <T> R<T> fail(int code, String msg) {
        return restResult(null, code, msg);
    }

    private static <T> R<T> restResult(T data, int code, String msg) {
        R<T> apiResult = new R<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public long getTime() {
        return time;
    }

    public void setTime(long time) {
        this.time = time;
    }


}
